{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 运算符\n",
    "\n",
    "\n",
    "**计算机**，顾名思义就是负责进行 **数学计算** 并且 **存储计算结果** 的电子设备\n",
    "\n",
    "## 目标\n",
    "\n",
    "* 运算符的基本使用\n",
    "\n",
    "## 01. 算数运算符\n",
    "\n",
    "* 算数运算符是 **运算符的一种**\n",
    "* 是完成基本的算术运算使用的符号，用来处理四则运算\n",
    "\n",
    "| 运算符| 描述 | 实例 |\n",
    "| :---: | :---: | --- |\n",
    "| + | 加 | 10 + 20 = 30 |\n",
    "| - | 减 | 10 - 20 = -10 |\n",
    "| * | 乘 | 10 * 20 = 200 |\n",
    "| / | 除 | 10 / 20 = 0.5 |\n",
    "| // | 取整除 | 返回除法的整数部分（商） 9 // 2 输出结果 4 |\n",
    "| % | 取余数 | 返回除法的余数 9 % 2 = 1 |\n",
    "| ** | 幂 | 又称次方、乘方，2 ** 3 = 8 |\n",
    "\n",
    "* 在 Python 中 `*` 运算符还可以用于字符串，计算结果就是字符串重复指定次数的结果\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'**************************************************'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"*\" * 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 10 + 20\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-10"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = 10 - 20\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-10"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " 10 * 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 / 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "9 // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "9 % 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 02. 算数运算符的优先级\n",
    "\n",
    "* 和数学中的运算符的优先级一致，在 Python 中进行数学计算时，同样也是：\n",
    "    * **先乘除后加减**\n",
    "    * 同级运算符是 **从左至右** 计算\n",
    "    * 可以使用 `()` 调整计算的优先级\n",
    "\n",
    "* 以下表格的算数优先级由高到最低顺序排列\n",
    "\n",
    "| 运算符 | 描述 |\n",
    "| --- | --- |\n",
    "| ** | 幂 (最高优先级) |\n",
    "| * / % // | 乘、除、取余数、取整除 |\n",
    "| + - | 加法、减法 |\n",
    "\n",
    "* 例如：\n",
    "    * `2 + 3 * 5 = 17`\n",
    "    * `(2 + 3) * 5 = 25`\n",
    "    * `2 * 3 + 5 = 11`\n",
    "    * `2 * (3 + 5) = 16`\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 3 * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 + 3) * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * 3 + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * (3 + 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 02. 比较（关系）运算符\n",
    "\n",
    "| 运算符 | 描述 |\n",
    "| --- | --- |\n",
    "| == | 检查两个操作数的值是否 **相等**，如果是，则条件成立，返回 True |\n",
    "| != | 检查两个操作数的值是否 **不相等**，如果是，则条件成立，返回 True |\n",
    "| > | 检查左操作数的值是否 **大于** 右操作数的值，如果是，则条件成立，返回 True |\n",
    "| < | 检查左操作数的值是否 **小于** 右操作数的值，如果是，则条件成立，返回 True |\n",
    "| >= | 检查左操作数的值是否 **大于或等于** 右操作数的值，如果是，则条件成立，返回 True |\n",
    "| <= | 检查左操作数的值是否 **小于或等于** 右操作数的值，如果是，则条件成立，返回 True |\n",
    "\n",
    "> Python 2.x 中判断 **不等于** 还可以使用 `<>` 运算符\n",
    "> \n",
    "> `!=` 在 Python 2.x 中同样可以用来判断 **不等于**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 == 1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1   > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "50 < 52"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "50 >= 52"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "50 <=52"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 03. 逻辑运算符\n",
    "\n",
    "| 运算符 | 逻辑表达式 | 描述 |\n",
    "| --- | --- | --- |\n",
    "| and | x and y | 只有 x 和 y 的值都为 True，才会返回 True<br />否则只要 x 或者 y 有一个值为 False，就返回 False |\n",
    "| or | x or y | 只要 x 或者 y 有一个值为 True，就返回 True<br />只有 x 和 y 的值都为 False，才会返回 False |\n",
    "| not | not x | 如果 x 为 True，返回 False<br />如果 x 为 False，返回 True |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 == 2 and 3 ==3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 == 2 or 3 == 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not 1 == 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 04. 赋值运算符\n",
    "\n",
    "* 在 Python 中，使用 `=` 可以给变量赋值\n",
    "* 在算术运算时，为了简化代码的编写，`Python` 还提供了一系列的 与 **算术运算符** 对应的 **赋值运算符**\n",
    "* 注意：**赋值运算符中间不能使用空格**\n",
    "\n",
    "| 运算符 | 描述 | 实例 |\n",
    "| --- | --- | --- |\n",
    "| = | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |\n",
    "| += | 加法赋值运算符 | c += a 等效于 c = c + a |\n",
    "| -= | 减法赋值运算符 | c -= a 等效于 c = c - a |\n",
    "| *= | 乘法赋值运算符\t | c *= a 等效于 c = c * a |\n",
    "| /= | 除法赋值运算符 | c /= a 等效于 c = c / a |\n",
    "| //= | 取整除赋值运算符 | c //= a 等效于 c = c // a |\n",
    "| %= | 取 **模** (余数)赋值运算符 | c %= a 等效于 c = c % a |\n",
    "| **= | 幂赋值运算符 | c **= a 等效于 c = c ** a |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 3 + 2\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = 50\n",
    "c += a\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c -= a\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "250"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c *= a\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.0"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c /= a \n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c //= a\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c %= a\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c *= a\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 05. 运算符的优先级\n",
    "\n",
    "* 以下表格的算数优先级由高到最低顺序排列\n",
    "\n",
    "| 运算符 | 描述 |\n",
    "| --- | --- |\n",
    "| ** | 幂 (最高优先级) |\n",
    "| * / % // | 乘、除、取余数、取整除 |\n",
    "| + - | 加法、减法 |\n",
    "| <= < > >= | 比较运算符 |\n",
    "| == != | 等于运算符 |\n",
    "| = %= /= //= -= += *= **= | 赋值运算符 |\n",
    "| not or and | 逻辑运算符 |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
